package com.example.erp.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.erp.entity.dto.ExcelDto;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class AnalysisExcelUtils {
    public static List<ExcelDto> analysisExcel(MultipartFile file) throws IOException {
        InputStream in = file.getInputStream();
        ExcelReader excelReader= ExcelUtil.getReader(in);
        List<List<Object>> list = excelReader.read(2);
        ArrayList objectList =new ArrayList<>();
        for (List<Object> row: list){
            ExcelDto excelDto=new ExcelDto();
            excelDto.setGoodsName(row.get(0).toString());
            excelDto.setLevel1Name(row.get(1).toString());
            excelDto.setLevel2Name(row.get(2).toString());
            excelDto.setUnits(row.get(3).toString());
            excelDto.setPrice(new BigDecimal( row.get(4).toString()));
            excelDto.setNum(Integer.parseInt(row.get(5).toString()));
            excelDto.setWeight(new BigDecimal( row.get(6).toString()));

            objectList.add(excelDto);
        }
        return objectList;
    }
}
